package com.example.moilapp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.example.moilapp.R;
import com.example.moilapp.adapter.Moil_Pending_Task_Adapter;

public class Moil_Image_Save_Service  extends AsyncTask<String, Void, String> {

	private ProgressDialog progdialod;
	private Activity activity;
	private Context context;
	
	Moil_Pending_Task_Adapter listAdapter;
	
    ExpandableListView expListView;
    List<String> listDataHeader = new ArrayList<String>();
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
	
	private ExpandableListView ExLstVw;
	
	private String JobNo;
	private String DoNo;
	private String encodedImg;
	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket) 
	private int timeoutSocket = 45000;
	
	private static byte[] buff =new byte[1024];
	private String retval="";
	
	private int[] app_ids;
	
	String pass_job_id="";

	JSONArray jArray;
	private static final String SOAP_ACTION = "http://tempuri.org/SaveImg";
	
    private static final String METHOD_NAME = "SaveImg";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://t2a1.cloudapp.net/TrafficWorkflow/TraficWorkflowWebService.asmx";
	private static final String TAG = null;
	
	public Moil_Image_Save_Service(Activity activity,String JobNo,String DoNo,String jobid,String encodedImg) {
		super();
		this.activity = activity;
		this.context = this.activity.getApplicationContext();
		this.JobNo = JobNo;
		this.DoNo = DoNo;
		this.pass_job_id = jobid;
		this.encodedImg = encodedImg;
		Log.e("encodedImg", encodedImg);
	}
	
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progdialod = ProgressDialog.show(this.activity, "Saving Image", "Saving",true,false);
	}
	
	
	@Override
	protected String doInBackground(String... urls) {
		
		 try {

	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	            Log.e("encodedImg", encodedImg);
	            
	            request.addProperty("JobID", pass_job_id);
	            request.addProperty("DoID", DoNo);
	            request.addProperty("Img", encodedImg);

	            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	            envelope.dotNet=true;
	            envelope.setOutputSoapObject(request);

	            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	            androidHttpTransport.call(SOAP_ACTION, envelope);
	            
	            SoapPrimitive resultSoapPrimitive;
	            resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
	                
	            String result = resultSoapPrimitive.toString();

	            retval = result;
	            
	            Log.d("return", retval);
	            
	            return retval;
	           
	        } catch (Exception e) {
	            
	       }
		 return retval;
		
	}
	
	@Override
	protected void onPostExecute(String result) {
		
		
		
		Log.d("Post Exec", "POST EXEC");
		
		/*Toast toast = Toast.makeText(activity, result, Toast.LENGTH_SHORT);
		toast.show();*/
		
		Log.d("Post tt", result);
		progdialod.dismiss();

		Log.d("Post Exec", result);
		
		try {
			
			JSONObject element01 = new JSONObject(result);
			String respond = element01.getString("SaveImgResult");

			Log.d("respond", respond);;
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		Bundle b = new Bundle();
		b.putString("status", "done");
		//b.putString("encodedStr", encodedStr);
		Intent intent = new Intent();
		intent.putExtras(b);
		activity.setResult(activity.RESULT_OK, intent);
		activity.finish();
		
	}

}